package com.example.k12.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.ViewModelProvider
import com.example.k12.R
import com.example.k12.bean.PayRes
import com.example.k12.dialog.InputPhoneDialog
import com.example.k12.dialog.PayResultDialog
import com.example.k12.dialog.WxFacePayDialog
import com.example.k12.utlis.DialogUtil
import com.example.k12.utlis.WxPayUtils
import com.tencent.wxpayface.IWxPayfaceCallback
import com.tencent.wxpayface.WxPayFace
import kotlin.coroutines.resume

class MainActivity : androidx.fragment.app.FragmentActivity() {

    // 声明 ViewModel 但不在这里初始化
    private lateinit var mainViewModel: MainViewModel

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 在 onCreate 中初始化 ViewModel
        mainViewModel = ViewModelProvider(this)[MainViewModel::class.java]
        DialogUtil.getInstance().showProgressDialog(this@MainActivity, "更新用户信息中...")
        mainViewModel.getDeviceInfo() { msg, isSuccess ->
            if (isSuccess) {
                DialogUtil.getInstance().showProgressDialog(this@MainActivity, msg)
                WxPayUtils.preloadEnv()
            } else {
                DialogUtil.getInstance().dismissDialog()
                PayResultDialog(this@MainActivity, PayRes(msg = msg, code = 1)).show()
            }
        }
//        findViewById<Button>(R.id.preloadEnvBtn).setOnClickListener {
//            WxPayUtils.preloadEnv()
//        }
        findViewById<Button>(R.id.btn_sync).setOnClickListener {
//            DialogUtil.getInstance().showProgressDialog(this@MainActivity, "更新用户信息中...")
//            WxPayUtils.manualUpdateFaceDatas() {
//                PayResultDialog(this@MainActivity, PayRes(msg = it ?: "", code = 1)).show()
//            }
//            mainViewModel.syncStatistics()
            WxPayFace.getInstance().startCodeScanner(WxPayUtils.params,object : IWxPayfaceCallback() {
                override fun response(info: MutableMap<Any?, Any?>?) {
                    Log.e("startCodeScanner","CheckoutViewModel - StartCodeScanner End $info")
                    Log.e("startCodeScanner","CheckoutViewModel doStartScanCode 3 $info")
                    val returnCode = info?.get("return_code")?.toString() ?: "ERROR"
                    Log.e("startCodeScanner","CheckoutViewModel - ScanCode $info $returnCode")
                    if (returnCode.compareTo("SUCCESS") == 0) {
                        Log.e("startCodeScanner",info?.get("code_msg")?.toString()!!)
                    } else {
                        Log.e("startCodeScanner","扫码失败")
                    }
                }
            })
        }
        findViewById<Button>(R.id.startVerifyBtn).setOnClickListener {
            WxFacePayDialog(this).show()
//            InputPhoneDialog(this, null,{
//                Log.e("MainActivity", "手机号后四位: $it")
//            }).show()
        }
        findViewById<EditText>(R.id.et_amt).addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {

            }

            override fun beforeTextChanged(
                p0: CharSequence?,
                p1: Int,
                p2: Int,
                p3: Int
            ) {
            }

            override fun onTextChanged(
                p0: CharSequence?,
                p1: Int,
                p2: Int,
                p3: Int
            ) {
                mainViewModel.totalAmount = try {
                    p0.toString().toDouble()
                } catch (e: Exception) {
                    0.01
                }
            }
        })

    }

}
